﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Lua.Serialization;
using OOAD_Project_Editor.Foundation.Script;
using System.Lua;
using System.IO;
using OOAD_Project_Editor.Foundation;
using OOAD_Project_Editor.Domain;

namespace OOAD_Project_Editor.Application
{
    class LoadStoreSceneHandler : GameComponent, ILoadStoreSceneHandler
    {
        public LoadStoreSceneHandler(Game game)
            : base(game) { }

        public bool Store(SceneScript script)
        {
            StringBuilder sb = new StringBuilder();
            LuaWriter writer = LuaWriter.Create(sb);
            LuaSerializer serializer = new LuaSerializer(typeof(SceneScript));
            SceneScript s = script;
            serializer.Serialize(writer, s);

            IFileHanlder file = new FileHanlder("lua");
            return file.SaveFile(sb);
        }

        public SceneScript Load()
        {
            IFileHanlder file = new FileHanlder("lua");
            string filePath = file.OpenFile();
            if (filePath == null)
                return null;

            LuaSerializer serializer = new LuaSerializer(typeof(SceneScript));

            SceneScript s = serializer.Deserialize(filePath) as SceneScript;
            return s;
        }
    }
}
